import { Component, OnInit } from '@angular/core';
import { I18nService } from '../../../core/i18n/i18n.service';
import { UserInfoService } from '../../../core/service';
import { Router } from '@angular/router';
import { fromEvent } from 'rxjs';
declare var  AMap: any; 
declare var  AMapUI: any; 

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.scss']
})
export class HomeComponent implements OnInit  {

  isLogin = false;
  username: string;

  constructor(
    private i18nService: I18nService,
    private userInfoService: UserInfoService,
    private router: Router
  ) { 
    // fromEvent(window,'pathSimplifierIns').subscribe((event) => {
    //     console.log(event)
    // });

  }
    // 地图要放到函数里。
    getMap(){
        let map = new AMap.Map('container', {
            resizeEnable: true,
            zoom:16,
            center: [106.480693, 29.591669] 
            
            // zoom: 16
        });
        // AMap.plugin('AMap.Geolocation', function() {
        //     var geolocation = new AMap.Geolocation({
        //         enableHighAccuracy: true,//是否使用高精度定位，默认:true
        //         timeout: 10000,          //超过10秒后停止定位，默认：5s
        //         buttonPosition:'RB',    //定位按钮的停靠位置
        //         buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
        //         zoomToAccuracy: true,   //定位成功后是否自动调整地图视野到定位点
    
        //     });
        var walkingOption = {}
         // 步行导航
        var walking = new AMap.Walking(walkingOption)
        //根据起终点坐标规划步行路线
        walking.search([106.480693, 29.591669], [106.482076,29.590515], function(status, result) {
            // result即是对应的步行路线数据信息，相关数据结构文档请参考  https://lbs.amap.com/api/javascript-api/reference/route-search#m_WalkingResult
            if (status === 'complete') {
                if (result.routes && result.routes.length) {
                    drawRoute(result.routes[0])
                    // log.success('绘制步行路线完成')
                }
            } else {
                // log.error('步行路线数据查询失败' + result)
            } 
        });
        function drawRoute (route) {
            var path = parseRouteToPath(route)
    
            var startMarker = new AMap.Marker({
                position: path[0],
                icon: 'https://webapi.amap.com/theme/v1.3/markers/n/start.png',
                map: map
            })
    
            var endMarker = new AMap.Marker({
                position: path[path.length - 1],
                icon: 'https://webapi.amap.com/theme/v1.3/markers/n/end.png',
                map: map
            })
    
            var routeLine = new AMap.Polyline({
                path: path,
                isOutline: true,
                outlineColor: '#ffeeee',
                borderWeight: 2,
                strokeWeight: 5,
                strokeColor: '#0091ff',
                lineJoin: 'round'
            })
    
            routeLine.setMap(map)
    
            // 调整视野达到最佳显示区域
            map.setFitView([ startMarker, endMarker, routeLine ])
        }
        function parseRouteToPath(route) {
            var path = []
    
            for (var i = 0, l = route.steps.length; i < l; i++) {
                var step = route.steps[i]
    
                for (var j = 0, n = step.path.length; j < n; j++) {
                  path.push(step.path[j])
                }
            }
    
            return path
        }
    
    };

  toPage(url: string) {
    this.router.navigateByUrl(url);
  }

  changeLang(lang: string) {
    this.i18nService.changeLang(lang);
  }

 
  ngOnInit() {
    this.getMap();

  }

}

 